s40723210 cd2020

  • Home
    • Site Map
    • reveal
    • blog
  • Weeks
    • Week1-5
      • OBS Streaming
    • Week6-9
      • Article impression
    • Week10-14
      • SSH setting
    • Week15-18
    • Weeks Meeting
  • 期末報告
  • Notes
    • Linux 指令
    • vi 與 vim 的指令整理
    • Q&A
  • Assignments
    • assignment1
      • Random grouping
      • Portable system
      • Four wheel car
    • assignment2
      • DigitalProductCollaboration
      • EngineeringProductDesign
      • MechanicalDesignProcess
      • Keyboard control car
    • assignment3
      • MSModelingAndTFApproaches
      • MechatronicDesignCases
      • MechaFutureAndChallenges
      • MechaEducFutureNeed
    • Final project
      • Car gripper Implement
  • CoppeliaSim
    • BubbleRob tutorial
    • Line following BubbleRob
    • External controller tutorial
    • Simulation
    • Simulation dialog
  • Webots Tutorial
    • User Interface
    • Scene Tree
MechanicalDesignProcess << Previous Next >> assignment3

Keyboard control car

V-rep 馬達驅動四輪車

V-rep 檔案連結: 4 wheel car.ttt

剛開始可以使用內建的功能來創建模型,Add > Primitive shape > Cuboid(需要的 形狀),給定所需形狀的參數值即可,創建出一個正方形本體,還有四個圓柱形輪子, 移動到相對應的位置,再來加上馬達 Add > Joint > Revolute,分別定位在四個輪 子的軸上,雙擊Revolute_joint圖示,在彈出的屬性框點擊Show dynamic parameters dialog,勾選 Motor enable,並設置 Target velocity 為 10,四個馬達都是如此。 

影片連結:https://youtu.be/y8DJ1sjuAqM


四輪車鍵盤控制轉向

V-rep 檔案連結: 4 wheel car keyboard.ttt

四輪車加上轉向及鍵盤控制,先開啟之前做好的四輪車,將兩個前輪馬達複製貼上,再使兩個馬達原地轉向90度,創建兩個小正方體,位置與前輪一模一樣,需要設定屬性避免與其他部件衝突,本體、輪子、小正方體都要關閉如下圖。



將控制迴路啟用,來控制轉向的限制角度,兩個馬達都用預設即可,如下圖設定。

將之前的馬達速度都關掉,點擊馬達圖示視窗下的show dynamic properties dialog,把motor properties的速度改為0,底下的lock motor when target velocity is zero是馬達速度為零時鎖定馬達,可開可不開。

最後把方塊拖移至對應馬達下,再整個放進本體下,把對應的前輪馬達放置方塊下,本體新增一個控制腳本,選擇cuboid,點擊菜單欄的Add > Associated child script > Threaded,將寫好的腳本複製貼上,更改前輪馬達的名子,這樣腳本才能控制,下圖為整個最終結構。

四輪車鍵盤控制轉向程式碼:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
-- This script is threaded! It is a very simple example of how Ackermann steering can be handled.
-- Normally, one would use a non-threaded script for that
 
threadFunction=function()
    while simGetSimulationState()~=sim_simulation_advancing_abouttostop do
        -- Read the keyboard messages (make sure the focus is on the main window, scene view):
        message,auxiliaryData=simGetSimulatorMessage()
        while message~=-1 do
            if (message==sim_message_keypress) then
                if (auxiliaryData[1]==2007) then
                    -- up key
                    desiredWheelRotSpeed=desiredWheelRotSpeed+wheelRotSpeedDx
                end
                if (auxiliaryData[1]==2008) then
                    -- down key
                    desiredWheelRotSpeed=desiredWheelRotSpeed-wheelRotSpeedDx
                end
                if (auxiliaryData[1]==2009) then
                    -- left key
                    desiredSteeringAngle=desiredSteeringAngle+steeringAngleDx
                    if (desiredSteeringAngle>45*math.pi/180) then
                        desiredSteeringAngle=45*math.pi/180
                    end
                end
                if (auxiliaryData[1]==2010) then
                    -- right key
                    desiredSteeringAngle=desiredSteeringAngle-steeringAngleDx
                    if (desiredSteeringAngle<-45*math.pi/180) then
                        desiredSteeringAngle=-45*math.pi/180
                    end
                end
            end
            message,auxiliaryData=simGetSimulatorMessage()
        end
 
        -- We handle the front left and right wheel steerings (Ackermann steering):
        steeringAngleLeft=math.atan(l/(-d+l/math.tan(desiredSteeringAngle)))
        steeringAngleRight=math.atan(l/(d+l/math.tan(desiredSteeringAngle)))
        simSetJointTargetPosition(steeringLeft,steeringAngleLeft)
        simSetJointTargetPosition(steeringRight,steeringAngleRight)
 
        -- We take care of setting the desired wheel rotation speed:
        simSetJointTargetVelocity(motorLeft,desiredWheelRotSpeed)
        simSetJointTargetVelocity(motorRight,desiredWheelRotSpeed)
 
        -- Since this script is threaded, don't waste time here:
        simSwitchThread() -- Resume the script at next simulation loop start
    end
end
 
-- Put some initialization code here:
-- Retrieving of some handles and setting of some initial values:
steeringLeft=simGetObjectHandle('Steer_left_joint')
steeringRight=simGetObjectHandle('Steer_right_joint')
motorLeft=simGetObjectHandle('Front_left_joint')
motorRight=simGetObjectHandle('Front_right_joint')
desiredSteeringAngle=0
desiredWheelRotSpeed=0
steeringAngleDx=2*math.pi/180
wheelRotSpeedDx=20*math.pi/180
d=0.755 -- 2*d=distance between left and right wheels
l=2.5772 -- l=distance between front and read wheels
 
-- Here we execute the regular thread code:
res,err=xpcall(threadFunction,function(err) return debug.traceback(err) end)
if not res then
    simAddStatusbarMessage('Lua runtime error: '..err)
end
 
-- Put some clean-up code here:

教學影片連結:https://youtu.be/0W1wmC1tr4A


MechanicalDesignProcess << Previous Next >> assignment3

Copyright © All rights reserved | This template is made with by Colorlib